package com.snack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snack.common.result.Result;
import com.snack.entity.pojo.Member;
import com.snack.entity.pojo.User;
import com.snack.mapper.MemberMapper;
import com.snack.service.MemberService;
import com.snack.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huangfuhao
 * @Date 2025/3/30
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {


    @Resource
    private UserService userService;

    @Override
    public Result addMember(Member member) {
        //根据电话号码查询会员数据是否存在
        Member member1 = lambdaQuery().eq(Member::getPhone, member.getPhone()).one();
        //如果存在则直接返回error且提示电话号码被使用
        if (member1 != null) {
            return Result.error("电话号码已被使用");
        }
        //若电话号码未被使用则可以将用户数据包括用户身份保存到users表
        User user = User.builder().roleId(4L).build();
        boolean save = userService.save(user);
        //拿到userId放到member对象同时初始化会员详细信息保存到member表当中
        Long userId = user.getId();
        member.setId(userId);
        member.setUserId(userId);
        member.setPoints(0);
        member.setLevel(1);
        member.setPhone(member.getPhone());
        boolean save1 = save(member);
        if (save1) {
            return Result.success(member);
        } else {
            return Result.error("保存会员信息失败");
        }
    }

    @Override
    public Result getMember(Member member) {
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        if (member.getPhone() != null) {
            queryWrapper.eq(Member::getPhone, member.getPhone());
        }
        if (member.getId() != null) {
            queryWrapper.eq(Member::getId, member.getId());
        }
        if (member.getUserId() != null) {
            queryWrapper.eq(Member::getUserId, member.getUserId());
        }
        if (member.getPoints() != null) {
            queryWrapper.eq(Member::getPoints, member.getPoints());
        }
        if (member.getLevel() != null) {
            queryWrapper.eq(Member::getLevel, member.getLevel());
        }

        Member one = getOne(queryWrapper);
        if (one != null) {
            return Result.success(one);
        } else {
            return Result.error("No member found");
        }
        
    }

    @Override
    public Result updateMember(Member member) {
        boolean updated = updateById(member);
        if (updated) {
            return Result.success(member);
        } else {
            return Result.error("Failed to update member");
        }
    }

    @Override
    public Result deleteMember(Member member) {
        boolean deleted = removeById(member.getId());
        if (deleted) {
            return Result.success("Member deleted successfully");
        } else {
            return Result.error("Failed to delete member");
        }
    }


    @Override
    public Result listMember(Member member) {
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();


        if (member.getPhone() != null) {
            //电话号码需要做模糊查询
            queryWrapper.like(Member::getPhone, member.getPhone());
        }
        if (member.getUserId() != null) {
            //会员用户id需要做模糊查询
            queryWrapper.like(Member::getUserId, member.getUserId());
        }
        if (member.getPoints() != null) {
            queryWrapper.eq(Member::getPoints, member.getPoints());
        }
        if (member.getLevel() != null) {
            queryWrapper.eq(Member::getLevel, member.getLevel());
        }

        List<Member> members = list(queryWrapper);
        if (members.isEmpty()) {
            return Result.error("No members found");
        } else {
            return Result.success(members);
        }
    }
}
